const axios = require("axios");
const { logger } = require("../logs/index");

exports.getHomeData = function (param) {
  return new Promise((resolve) => {
    axios({
      url: "https://pbaccess.video.qq.com/trpc.vector_layout.page_view.PageService/getPage?video_appid=3000010",
      headers: {
        "access-control-allow-credentials": true,
        cookie: global.cookieStr,
      },
      method: "POST",
      data: {
        page_context: param.page_context || null,
        page_params: {
          page_id: param.page_id,
          page_type: "channel",
        },
        page_bypass_params: {
          params: {
            caller_id: "3000010",
            data_mode: "default",
            page_id: param.page_id,
            page_type: "channel",
            platform_id: "2",
            user_mode: "default",
          },
          scene: "channel",
          abtest_bypass_id: global.cookie.video_guid || "",
        },
      },
    })
      .then((res) => {
        let data = {
          list: [],
          pc_carousel: [],
          pc_hot_filter: [],
        };
        let resData = res.data.data;
        if (resData.CardList) {
          let CardList = res.data.data.CardList;
          let pc_videoList = [];
          if (Array.isArray(CardList) && CardList.length > 0) {
            CardList.forEach((item) => {
              if (item.type === "pc_carousel") {
                let list = [];
                item.children_list.list.cards.forEach((ele) => {
                  if (ele.params.cid || ele.params.vid) {
                    list.push({
                      cid: ele.params.cid || "",
                      image_url:
                        ele.params.image_url ||
                        ele.params.pic_1080x607 ||
                        ele.params.pic_1280x720 ||
                        ele.params.pic_1920x800 ||
                        ele.params.pic_ori_2880x900,
                      logo_url: ele.params.pic_pc_ip_logo || "",
                      title: ele.params.title,
                      label: ele.params.topic_label || "",
                      vid: ele.params.vid || "",
                      desc: ele.params.stitle_pc || "",
                    });
                  }
                });
                data.pc_carousel = list;
              }
              if (item.type === "pc_hot_filter") {
                let list = [];
                item.children_list.list.cards.forEach((ele) => {
                  list.push(ele.params);
                });
                data.pc_hot_filter = list;
              }
              if (item.type === "pc_shelves") {
                let list = [];
                item.children_list.list.cards.forEach((ele) => {
                  if (ele.params.cid || ele.params.vid) {
                    list.push({
                      cid: ele.params.cid || "",
                      image_url:
                        ele.params.image_url_vertical ||
                        ele.params.image_url ||
                        ele.params.ready_image_url,
                      logo_url: ele.params.pic_pc_ip_logo || "",
                      title: ele.params.title || "",
                      label: ele.params.topic_label || "",
                      vid: ele.params.vid || "",
                      desc:
                        ele.params.rec_normal_reason || ele.params.rec_subtitle,
                    });
                  }
                });
                data.list.push({
                  type: "pc_shelves",
                  name: item.params.ZT_leaf_head || "",
                  list: list,
                });
              }
              if (item.type === "pc_video") {
                let video =
                  item.children_list &&
                  item.children_list.list &&
                  item.children_list.list.cards &&
                  item.children_list.list.cards[0];
                if (video && (video.params.cid || video.params.vid)) {
                  pc_videoList.push({
                    cid: video.params.cid || "",
                    image_url:
                      video.params.image_url_vertical ||
                      video.params.vid_image_url,
                    title: video.params.title || "",
                    label: video.params.topic_label || "",
                    vid: video.params.vid || "",
                    desc:
                      video.params.second_title ||
                      video.params.rec_normal_reason ||
                      video.params.rec_subtitle,
                  });
                }
              }
            });
          }
          if (!param.page_context) {
            data.list.push({
              type: "pc_video",
              name: "为你推荐",
              list: pc_videoList,
            });
          } else {
            data.list = pc_videoList;
          }

          data.has_next_page = resData.has_next_page;
          data.page_context = resData.page_context;
          resolve({
            code: true,
            data,
          });
        } else {
          resolve({
            code: false,
            data: "",
          });
        }
      })
      .catch((res) => {
        resolve({
          code: false,
          data: "",
        });
      });
  });
};

exports.getFilterList = function (param) {
  let data = {
    page_context: { page_index: param.page_index.toString() },
    page_params: {
      page_id: param.page_id,
      page_type: "operation",
      channel_id: param.channel_id,
      filter_params: param.filter_params,
      page: param.page_index.toString(),
    },
    page_bypass_params: {
      params: {
        page_id: param.page_id,
        page_type: "operation",
        channel_id: param.channel_id,
        filter_params: param.filter_params,
        page: param.page_index.toString(),
        caller_id: "3000010",
        platform_id: "2",
        data_mode: "default",
        user_mode: "default",
      },
      scene: "operation",
      abtest_bypass_id: global.cookie.video_guid || "",
    },
  };
  return new Promise((resolve) => {
    axios({
      url: "https://pbaccess.video.qq.com/trpc.vector_layout.page_view.PageService/getPage?video_appid=3000010",
      headers: {
        "access-control-allow-credentials": true,
        cookie: global.cookieStr,
      },
      data,
      method: "POST",
    })
      .then((res) => {
        let resData = res.data.data || {};
        if (resData.CardList && Array.isArray(resData.CardList)) {
          let data = [];
          resData.CardList.forEach((element) => {
            if (element.type === "channel_list_filter") {
              data =
                element.children_list &&
                element.children_list.list &&
                element.children_list.list.cards.map((item) => item.params);
            }
          });
          resolve({
            code: true,
            data: data,
          });
        } else {
          resolve({
            code: false,
            data: "",
          });
        }
      })
      .catch((res) => {
        resolve({
          code: false,
          data: "",
        });
      });
  });
};

exports.getFilterVideo = function (param) {
  let data = {
    page_context: { page_index: param.page_index.toString() },
    page_params: {
      page_id: param.page_id,
      page_type: "operation",
      channel_id: param.channel_id,
      filter_params: param.filter_params,
      page: param.page_index.toString(),
    },
    page_bypass_params: {
      params: {
        page_id: param.page_id,
        page_type: "operation",
        channel_id: param.channel_id,
        filter_params: param.filter_params,
        page: param.page_index.toString(),
        caller_id: "3000010",
        platform_id: "2",
        data_mode: "default",
        user_mode: "default",
      },
      scene: "operation",
      abtest_bypass_id: global.cookie.video_guid || "",
    },
  };
  return new Promise((resolve) => {
    axios({
      url: "https://pbaccess.video.qq.com/trpc.vector_layout.page_view.PageService/getPage?video_appid=3000010",
      headers: {
        "access-control-allow-credentials": true,
        cookie: global.cookieStr,
      },
      method: "POST",
      data,
    })
      .then((res) => {
        let resData = res.data.data || {};
        if (resData.CardList && Array.isArray(resData.CardList)) {
          let data = [];
          resData.CardList.forEach((element) => {
            if (element.type === "channel_list_poster") {
              let list =
                (element.children_list &&
                  element.children_list.list &&
                  element.children_list.list.cards) ||
                [];
              list.forEach((item) => {
                data.push({
                  cid: item.params.cid || "",
                  title: item.params.title || item.params.series_name || "",
                  desc: item.params.second_title || "",
                  image_url: item.params.new_pic_vt || "",
                });
              });
            }
          });
          resolve({
            code: true,
            data: {
              list: data || [],
              has_next_page: resData.resData,
            },
          });
        } else {
          resolve({
            code: false,
            data: "",
          });
        }
      })
      .catch((res) => {
        resolve({
          code: false,
          data: "",
        });
      });
  });
};

exports.getVideoDesc = function (param) {
  let data = {
    page_params: {
      req_from: "web",
      cid: param.cid,
      vid: param.vid,
      lid: "",
      page_type: "detail_operation",
      page_id: "detail_page_introduction",
    },
    has_cache: 1,
  };
  return new Promise((resolve) => {
    axios({
      url: "https://pbaccess.video.qq.com/trpc.universal_backend_service.page_server_rpc.PageServer/GetPageData?video_appid=3000010&vplatform=2",
      headers: {
        "access-control-allow-credentials": true,
        cookie: global.cookieStr,
        origin: " https://v.qq.com",
        referer: "https://v.qq.com/",
      },
      method: "POST",
      data,
    })
      .then((res) => {
        try {
          let resData = res.data.data || {};
          let data = {};
          let desc =
            resData.module_list_datas[0].module_datas[0].item_data_lists
              .item_datas[0];
          data.title = desc.item_params.title;
          data.desc =
            desc.item_params.cover_description ||
            desc.item_params.video_description ||
            "暂无简介";
          data.episode_all = desc.item_params.episode_all || 0;
          data.detail_info =
            desc.item_params.detail_info || desc.item_params.sub_genre;
          data.image_url = desc.new_pic_hz || "";
          resolve({
            code: true,
            data,
          });
        } catch (error) {
          logger.error(res);
          resolve({
            code: false,
            data: "",
          });
        }
      })
      .catch((res) => {
        logger.error(res);
        resolve({
          code: false,
          data: "",
        });
      });
  });
};

exports.getVideoList = function (param) {
  let data = {
    page_params: {
      req_from: "web_vsite",
      page_id: "vsite_episode_list",
      page_type: "detail_operation",
      id_type: "1",
      page_size: "60",
      cid: param.cid,
      vid: "",
      lid: "",
      page_num: "",
      page_context: param.page_context || "",
      detail_page_type: "1",
    },
    has_cache: 1,
  };
  return new Promise((resolve) => {
    axios({
      url: "https://pbaccess.video.qq.com/trpc.universal_backend_service.page_server_rpc.PageServer/GetPageData?video_appid=3000010&vplatform=2",
      headers: {
        "access-control-allow-credentials": true,
        cookie: global.cookieStr,
        origin: " https://v.qq.com",
        referer: "https://v.qq.com/",
      },
      method: "POST",
      data,
    })
      .then((res) => {
        try {
          let resData = res.data.data || {};
          let data = {};
          let list =
            resData.module_list_datas[0].module_datas[0].item_data_lists
              .item_datas;
          data.list = list.map((item) => {
            {
              return {
                vid: item.item_params.vid,
                image_url: item.item_params.image_url,
                play_title: item.item_params.play_title,
                index: item.item_params.title,
              };
            }
          });
          data.page_context =
            resData.module_list_datas[0].module_datas[0].module_params.page_context;
          resolve({
            code: true,
            data: data,
          });
        } catch (error) {
          console.log(error);
          logger.err(error);
          resolve({
            code: false,
            data: "",
          });
        }
      })
      .catch((res) => {
        logger.error(res);
        resolve({
          code: false,
          data: "",
        });
      });
  });
};

exports.getVideoWord = function (param) {
  return new Promise((resolve) => {
    let rltnum = param.rltnum || 10;
    let key = param.key;
    axios
      .get("https://suggest.video.iqiyi.com/", {
        params: {
          rltnum,
          key,
          platform: 11,
        },
      })
      .then((res) => {
        try {
          let resData = res.data.data || [];
          let data = resData.map((item) => item.name);
          resolve({
            code: true,
            data,
          });
        } catch (error) {
          resolve({
            code: false,
            data: "",
          });
        }
      })
      .catch((res) => {
        resolve({
          code: false,
          data: "",
        });
      });
  });
};

exports.getVideoSearchList = function (param) {
  return new Promise((resolve) => {
    axios
      .get(
        "https://pcw-api.iqiyi.com/strategy/pcw/data/soResultListV2LeftSide",
        {
          params: {
            pageNum: param.pageNum || 20,
            key: param.key,
          },
        }
      )
      .then((res) => {
        try {
          let resData = res.data.data || {};
          let data = resData.formatData || {};
          resolve({
            code: true,
            data,
          });
        } catch (error) {
          resolve({
            code: false,
            data: "",
          });
        }
      })
      .catch((res) => {
        resolve({
          code: false,
          data: "",
        });
      });
  });
};

exports.getVideoPlayUrl=async()=>{
  
}
